//! SHA哈希算法实现
//! 
//! 提供SHA1、SHA256、SHA512哈希功能，对应Java的SHAUtils

use sha1::Sha1;
use sha2::{Sha256, Sha512, Digest};

/// SHA1加密
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha1_hash;
/// 
/// let hash = sha1_hash("hello world");
/// assert_eq!(hash, "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
/// ```
pub fn sha1_hash(input: &str) -> String {
    let mut hasher = Sha1::new();
    hasher.update(input.as_bytes());
    let result = hasher.finalize();
    format!("{:x}", result)
}

/// SHA1加密（字节输入）
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha1_hash_bytes;
/// 
/// let hash = sha1_hash_bytes(b"hello world");
/// assert_eq!(hash, "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
/// ```
pub fn sha1_hash_bytes(input: &[u8]) -> String {
    let mut hasher = Sha1::new();
    hasher.update(input);
    let result = hasher.finalize();
    format!("{:x}", result)
}

/// SHA256加密
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha256_hash;
/// 
/// let hash = sha256_hash("hello world");
/// assert_eq!(hash, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
/// ```
pub fn sha256_hash(input: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(input.as_bytes());
    let result = hasher.finalize();
    format!("{:x}", result)
}

/// SHA256加密（字节输入）
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha256_hash_bytes;
/// 
/// let hash = sha256_hash_bytes(b"hello world");
/// assert_eq!(hash, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
/// ```
pub fn sha256_hash_bytes(input: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(input);
    let result = hasher.finalize();
    format!("{:x}", result)
}

/// SHA512加密
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha512_hash;
/// 
/// let hash = sha512_hash("hello world");
/// assert!(hash.len() == 128); // SHA512 produces 64-byte hash = 128 hex chars
/// ```
pub fn sha512_hash(input: &str) -> String {
    let mut hasher = Sha512::new();
    hasher.update(input.as_bytes());
    let result = hasher.finalize();
    format!("{:x}", result)
}

/// SHA512加密（字节输入）
/// 
/// # Examples
/// ```
/// use librarys::crypto::hash::sha512_hash_bytes;
/// 
/// let hash = sha512_hash_bytes(b"hello world");
/// assert!(hash.len() == 128);
/// ```
pub fn sha512_hash_bytes(input: &[u8]) -> String {
    let mut hasher = Sha512::new();
    hasher.update(input);
    let result = hasher.finalize();
    format!("{:x}", result)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sha_hashes() {
        assert_eq!(sha1_hash("hello world"), "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
        assert_eq!(sha256_hash("hello world"), "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
        
        let sha512_result = sha512_hash("hello world");
        assert_eq!(sha512_result.len(), 128); // SHA512 = 64 bytes = 128 hex chars
        
        // 测试字节输入
        assert_eq!(sha1_hash_bytes(b"hello world"), sha1_hash("hello world"));
        assert_eq!(sha256_hash_bytes(b"hello world"), sha256_hash("hello world"));
        assert_eq!(sha512_hash_bytes(b"hello world"), sha512_hash("hello world"));
    }
}